package com.chen.palmar.project.set;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.chen.palmar.R;
import com.chen.palmar.base.BaseActivity;
import com.chen.palmar.common.datasource.DataCenter;
import com.chen.palmar.common.override.HttpSubscriber;
import com.chen.palmar.common.widget.view.AutoToolbar;
import com.chen.palmar.common.widget.view.CircleImageView;
import com.chen.palmar.entity.ApplyManagerEntity;
import com.chen.palmar.project.home.ApplyEditActivity;
import com.chen.palmar.project.home.ApplyInfoActivity;
import com.primb.androidlibs.net.entity.HttpResultEntity;
import com.primb.androidlibs.ui.recyclerview.BaseQuickAdapter;
import com.primb.androidlibs.ui.recyclerview.BaseViewHolder;
import com.squareup.picasso.Picasso;

import java.util.HashMap;

import butterknife.Bind;
import rx.Subscription;

/**
 * @author Chen
 * @date 2018/1/9
 * 功能描述：
 */

public class ApplyListActivity extends BaseActivity {
    public static final int REQUEST_CODE_EDIT = 3;
    @Bind(R.id.title_bar)
    TextView titleBar;
    @Bind(R.id.tool_bar)
    AutoToolbar toolBar;
    @Bind(R.id.rv_apply_list)
    RecyclerView rvApplyList;
    private BaseQuickAdapter<ApplyManagerEntity.DataBean, BaseViewHolder> adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        init();
    }

    @Override
    public void init() {
        toolBar.setNavigationOnClickListener(v -> ApplyListActivity.this.finish());

        titleBar.setText("发布管理");

        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        rvApplyList.setLayoutManager(layoutManager);
        adapter = new BaseQuickAdapter<ApplyManagerEntity.DataBean, BaseViewHolder>(R.layout.item_apply_list) {
            @Override
            protected void convert(BaseViewHolder helper, ApplyManagerEntity.DataBean item) {
                helper.setText(R.id.tv_item_apply_content, item.getContent());
                helper.setText(R.id.tv_item_apply_name, item.getTitle());
                ImageView head = (ImageView) helper.getView(R.id.iv_item_apply_head);
                Picasso.with(ApplyListActivity.this).load(item.getImg()).placeholder(R.mipmap.occupy_icon).error(R.mipmap.occupy_icon).into(head);

                helper.addOnClickListener(R.id.tv_item_apply_refresh);
                helper.addOnClickListener(R.id.iv_item_apply_refresh);
                helper.addOnClickListener(R.id.tv_item_apply_top);
                helper.addOnClickListener(R.id.iv_item_apply_top);
                helper.addOnClickListener(R.id.tv_item_apply_edit);
                helper.addOnClickListener(R.id.iv_item_apply_edit);
                helper.addOnClickListener(R.id.tv_item_apply_delete);
                helper.addOnClickListener(R.id.iv_item_apply_delete);
            }
        };
        adapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                switch (view.getId()) {
                    case R.id.tv_item_apply_refresh:
                    case R.id.iv_item_apply_refresh:
                        getListData();
                        break;
                    case R.id.tv_item_apply_top:
                    case R.id.iv_item_apply_top:
                        break;
                    case R.id.tv_item_apply_edit:
                    case R.id.iv_item_apply_edit:
                        Intent intent = new Intent(ApplyListActivity.this, ApplyEditActivity.class);
                        intent.putExtra("info", ((ApplyManagerEntity.DataBean) adapter.getData().get(position)).getType() + "");
                        intent.putExtra("entity", ((ApplyManagerEntity.DataBean) adapter.getData().get(position)));
                        startActivityForResult(intent, REQUEST_CODE_EDIT);
                        break;
                    case R.id.tv_item_apply_delete:
                    case R.id.iv_item_apply_delete:
                        deleteApply(((ApplyManagerEntity.DataBean) adapter.getData().get(position)).getArticleId() + "", position);
                        break;
                }
            }
        });
        rvApplyList.setAdapter(adapter);

        getListData();
    }

    private void deleteApply(String id, int position) {
        HashMap<String, String> map = new HashMap<>();
        map.put("article_id", id);
        Subscription subscribe = DataCenter.deleteApply(map).subscribe(new HttpSubscriber<HttpResultEntity>(this, showLoading()) {

            @Override
            public void onNext(HttpResultEntity entity) {
                showToast(entity.getMessage());
                getListData();
                adapter.notifyDataSetChanged();
            }
        });
        subscription.add(subscribe);
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_apply_list;
    }

    private void getListData() {
        Subscription subscribe = DataCenter.applyManagerList().subscribe(new HttpSubscriber<ApplyManagerEntity>(this, showLoading()) {

            @Override
            public void onNext(ApplyManagerEntity entity) {
                adapter.setNewData(entity.getData());
            }
        });
        subscription.add(subscribe);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_CODE_EDIT && resultCode == Activity.RESULT_OK) {
            getListData();
        }
    }
}
